/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using MyPhotoIndex.ImageBank;
using System.Collections.ObjectModel;
using MyPhotoIndex.Interfaces;
using MyPhotoIndex.Interfaces.Imaging;

namespace MyPhotoIndex
{
    public class SelectedImageCollection : IImageCollection
    {
        private IImageCollection            m_imageCollection;
        private List<IImageData>            m_imageDataList = new List<IImageData>();
        private Dictionary<int, IImageData> m_imageDataDictionary = new Dictionary<int,IImageData>();

        public SelectedImageCollection(IImageCollection imageCollection)
        {
            m_imageCollection = imageCollection;
        }

        public SelectedImageCollection(ReadOnlyCollection<int> selectedList, IImageCollection imageCollection):
            this(imageCollection)
        {

            AddRange(selectedList);
        }

        #region IImageCollection Members

        public IImageData this[int imageId]
        {
            get
            {
                return m_imageCollection[imageId];
            }
        }

        public int Count
        {
            get 
            {
                return m_imageDataList.Count;
            }
        }

        #endregion

        public void Add(int imageId)
        {
            IImageData imageData = m_imageCollection[imageId];
            if (m_imageDataDictionary.ContainsKey(imageId) == false)
            {
                m_imageDataList.Add(imageData);
                m_imageDataDictionary.Add(imageData.Id, imageData);
            }
        }

        internal void AddRange(IEnumerable<int> imageIds)
        {
            foreach (int imageId in imageIds)
            {
                Add(imageId);
            }
        }

        public void Remove(int imageId)
        {
            IImageData imageToRemove = m_imageCollection[imageId];
            if (imageToRemove != null &&
                m_imageDataDictionary.ContainsKey(imageId) == true)
            {
                m_imageDataList.Remove(imageToRemove);
                m_imageDataDictionary.Remove(imageId);
            }
        }

        public void Remove(int imageId, bool deleteFromMedia)
        {
            throw new NotImplementedException(Resources.Resources.MethodIsNotImplemented);
        }

        internal void Clear()
        {
            m_imageDataList.Clear();
            m_imageDataDictionary.Clear();
        }

        public IImageData[] ToArray()
        {
            return m_imageDataList.ToArray();
        }

        #region IEnumerable<ImageData> Members

        public IEnumerator<IImageData> GetEnumerator()
        {
            return m_imageDataList.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        #region IImageCollection Members

        public event EventHandler<EventArgs> ImageCollectionLoadedEvent;

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool dispose)
        {
        }

        #endregion
    }
}
